/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager.model;

import Golf_Manager.vo.Group;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrator
 */
public class GroupModel extends AbstractTableModel { 
    
    Class[] types = new Class [] { java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class};
    private List<Golf_Manager.vo.Group> groups = new ArrayList<>();
    private String[] columns = new String [] {"조이름", "조원1", "조원2", "조원3", "조원4"};
    private boolean[] canEdit = new boolean [] { true, false, false, false, false};
    
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return types[columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];
    }

    @Override
    public int getRowCount() {
        return groups.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }
    
    @Override
    public String getColumnName(int column) {
        return columns[column];
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getGroupName : " + groups.get(rowIndex).getGroupName());
                return groups.get(rowIndex).getGroupName();
            }
            case 1: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getMemberDesc1 : " + groups.get(rowIndex).getMemberDesc1());
                return groups.get(rowIndex).getMemberDesc1();
            }
            case 2: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getMemberDesc2 : " + groups.get(rowIndex).getMemberDesc2());
                return groups.get(rowIndex).getMemberDesc2();
            }
            case 3: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getMemberDesc3 : " + groups.get(rowIndex).getMemberDesc3());
                return groups.get(rowIndex).getMemberDesc3();
            }
            case 4: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getMemberDesc4 : " + groups.get(rowIndex).getMemberDesc4());
                return groups.get(rowIndex).getMemberDesc4();
            }
            default: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " default : " + null);
                return null;
            }
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//        System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " aValue : " + aValue);
        switch(columnIndex){
            case 0: {
                groups.get(rowIndex).setGroupName((String)aValue); break;
            }
        }
        fireTableDataChanged();
    }

    public List<Group> getGroups() {
        return groups;
    }

    public void setGroups(List<Group> groups) {
        this.groups = new ArrayList<>(groups);
        fireTableDataChanged();
    }
    
    public Group getGroup(int rowIndex) {
        return groups.get(rowIndex);
    }

    public void setGroupMember(int rowIndex, int columnIndex, int joinMemberRow, String memberSeq, String memberDesc) {
        switch(columnIndex){
            case 1: {
                this.groups.get(rowIndex).setMemberRow1(joinMemberRow);
                this.groups.get(rowIndex).setMemberSeq1(memberSeq);
                this.groups.get(rowIndex).setMemberDesc1(memberDesc);
                break;
            }
            case 2: {
                this.groups.get(rowIndex).setMemberRow2(joinMemberRow);
                this.groups.get(rowIndex).setMemberSeq2(memberSeq);
                this.groups.get(rowIndex).setMemberDesc2(memberDesc);
                break;
            }
            case 3: {
                this.groups.get(rowIndex).setMemberRow3(joinMemberRow);
                this.groups.get(rowIndex).setMemberSeq3(memberSeq);
                this.groups.get(rowIndex).setMemberDesc3(memberDesc);
                break;
            }
            case 4: {
                this.groups.get(rowIndex).setMemberRow4(joinMemberRow);
                this.groups.get(rowIndex).setMemberSeq4(memberSeq);
                this.groups.get(rowIndex).setMemberDesc4(memberDesc);
                break;
            }
        }
        fireTableDataChanged();
    }
    
    public int getMemberRow(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 1: {
                return this.groups.get(rowIndex).getMemberRow1();
            }
            case 2: {
                return this.groups.get(rowIndex).getMemberRow2();
            }
            case 3: {
                return this.groups.get(rowIndex).getMemberRow3();
            }
            case 4: {
                return this.groups.get(rowIndex).getMemberRow4();
            }
            default: return -1;
        }
    }
    
    public void setCanEdit(int column, Boolean flag) {
        this.canEdit[column] = flag;
    }
}
